﻿using System.Collections.Generic;
using System.ComponentModel;
using System.ServiceModel.Activation;
using UFIDA.U9.Base.Language;
using UFIDA.U9.Base.Language.Proxy;
using UFIDA.U9.Base.UserRole;
using UFIDA.U9.Base.UserRole.Proxy;
using UFIDA.U9.WSS.ProxyService.Exceptions;
using UFIDA.U9.WSS.ProxyService.Models;
using UFIDA.U9.WSS.ProxyService.Utils;
using UFIDA.U9.WSS.SystemService.Interfaces;
using UFIDA.U9.WSS.SystemService.Models;
using UFIDA.UBF.SystemManage;
using UFIDA.UBF.SystemManage.Agent;
using UFSoft.UBF.Util.Context;

namespace UFIDA.U9.WSS.SystemService.Services
{
    /// <summary>
    ///     系统服务
    /// </summary>
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class SystemService : ISystemService
    {
        /// <summary>
        ///     测试连接
        /// </summary>
        /// <returns></returns>
        [Description("测试连接")]
        public ReturnMessage<bool> TestConnection()
        {
            ReturnMessage<bool> ret = new ReturnMessage<bool>();
            ret.Data = true;
            return ret;
        }

        /// <summary>
        ///     获取所有企业
        /// </summary>
        /// <returns></returns>
        [Description("获取所有企业")]
        public ReturnMessage<List<EnterpriseData>> GetEnterprises()
        {
            ReturnMessage<List<EnterpriseData>> ret = new ReturnMessage<List<EnterpriseData>>();
            List<EnterpriseData> dataList = new List<EnterpriseData>();
            EnterpriseInfo enterpriseInfo = new EnterpriseInfo();
            List<Enterprise> allEnterprise = enterpriseInfo.GetAllEnterprise();
            foreach (Enterprise enterprise in allEnterprise)
            {
                EnterpriseData data = new EnterpriseData();
                data.Code = enterprise.Code;
                data.Name = enterprise.Name;
                dataList.Add(data);
            }

            ret.Data = dataList;
            return ret;
        }

        /// <summary>
        ///     获取语言
        /// </summary>
        /// <returns></returns>
        [Description("获取语言")]
        public ReturnMessage<List<LanguageData>> GetLanguages(string enterpriseID)
        {
            if (string.IsNullOrEmpty(enterpriseID))
                throw new WSException(ExceptionCodes.ServiceRequestDataFormatException,
                    "EnterpriseID can not be empty");
            using (new SystemWritablePolicy())
            {
                PlatformContext.Current.EnterpriseID = enterpriseID;
            }

            ReturnMessage<List<LanguageData>> ret = new ReturnMessage<List<LanguageData>>();
            GetLanguageInfoProxy getLanguageInfoProxy = new GetLanguageInfoProxy();
            List<LanguageInfoDataTransferObjectData> dtoList = getLanguageInfoProxy.Do();
            List<LanguageData> dataList = new List<LanguageData>();
            foreach (LanguageInfoDataTransferObjectData languageInfoDataTransferObjectData in dtoList)
            {
                if (languageInfoDataTransferObjectData.Code == "EffectiveCount") continue;
                if (!languageInfoDataTransferObjectData.IsSystem) continue;
                LanguageData codeNameDTO = new LanguageData();
                codeNameDTO.Code = languageInfoDataTransferObjectData.Code;
                codeNameDTO.Name = languageInfoDataTransferObjectData.Name;
                dataList.Add(codeNameDTO);
            }

            ret.Data = dataList;
            return ret;
        }

        /// <summary>
        ///     获取用户组织列表
        /// </summary>
        /// <returns></returns>
        [Description("获取用户组织列表")]
        public ReturnMessage<List<OrgData>> GetOrgsByUserCode(string enterpriseID, string userCode, string culture)
        {
            if (string.IsNullOrEmpty(enterpriseID))
                throw new WSException(ExceptionCodes.ServiceRequestDataFormatException,
                    "enterpriseID can not be empty ");
            if (string.IsNullOrEmpty(enterpriseID))
                throw new WSException(ExceptionCodes.ServiceRequestDataFormatException, "userCode can not be empty ");
            ReturnMessage<List<OrgData>> ret = new ReturnMessage<List<OrgData>>();
            ret.Data = new List<OrgData>();
            using (new SystemWritablePolicy())
            {
                PlatformContext.Current.EnterpriseID = enterpriseID;
                PlatformContext.Current.Culture = string.IsNullOrEmpty(culture)
                    ? Constant.DefaultContextCulture
                    : culture;
                PlatformContext.Current.Default_Culture = string.IsNullOrEmpty(culture)
                    ? Constant.DefaultContextCulture
                    : culture;
            }

            GetLanguageInfoProxy getLanguageInfoProxy = new GetLanguageInfoProxy();
            List<LanguageInfoDataTransferObjectData> languageDTOList = getLanguageInfoProxy.Do();
            List<string> supportCultureList = new List<string>();
            foreach (LanguageInfoDataTransferObjectData languageInfoDataTransferObjectData in languageDTOList)
            {
                if (languageInfoDataTransferObjectData.Code == "EffectiveCount") continue;
                if (!languageInfoDataTransferObjectData.IsSystem) continue;
                supportCultureList.Add(languageInfoDataTransferObjectData.Code);
            }

            using (new SystemWritablePolicy())
            {
                PlatformContext.Current.Support_CultureName_List = string.Join(",", supportCultureList);
            }

            GetOrgsByUserCodeProxy getOrgsByUserCodeProxy = new GetOrgsByUserCodeProxy();
            getOrgsByUserCodeProxy.UserCode = userCode;
            List<OrganizationDataTransferObjectData> orgDTOList = getOrgsByUserCodeProxy.Do();
            if (orgDTOList != null && orgDTOList.Count > 0)
            {
                foreach (OrganizationDataTransferObjectData objectData in orgDTOList)
                {
                    OrgData orgData = new OrgData();
                    orgData.ID = objectData.Code;
                    orgData.Code = objectData.OrgCode;
                    orgData.Name = objectData.Name;
                    ret.Data.Add(orgData);
                }
            }

            return ret;
        }

        [Description("登录")]
        public ReturnMessage<bool> Login(ContextInfo context)
        {
            if (context == null)
                throw new WSException(ExceptionCodes.ServiceRequestDataFormatException, "context is null");
            ReturnMessage<bool> ret = new ReturnMessage<bool>();
            context.ValidateAndInit(true);
            ret.Data = true;
            return ret;
        }
    }
}